/**
 * 
 */
package com.yidu.drug.service.impl;

import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.google.gson.Gson;
import com.yidu.drug.common.Page;
import com.yidu.drug.common.Tools;
import com.yidu.drug.dao.DrugMaterialsModelMapper;
import com.yidu.drug.dao.MaterialTypeModelMapper;
import com.yidu.drug.dao.MaterialsModelMapper;
import com.yidu.drug.dao.RepertoryModelMapper;
import com.yidu.drug.model.MaterialTypeModel;
import com.yidu.drug.model.MaterialsModel;
import com.yidu.drug.model.RepertoryModel;
import com.yidu.drug.service.MaterialsService;

/**
 * 材料的service实现类
 * @author 王志伟
 * @date 2018年8月9日 下午2:15:46
 * @version 1.0
 */
@Service
public class MaterialsServiceImpl implements MaterialsService{
	
	//材料dao
	@Resource
	private MaterialsModelMapper materialsDao;
	
	//库存dao
	@Resource
	private RepertoryModelMapper repDao;
	
	//材料类型dao
	@Resource
	private MaterialTypeModelMapper materialTypeDao;
	
	//药品材料DAO
	@Resource
	private DrugMaterialsModelMapper dmDao;
	/**
	 * 根据材料ID删除
	 */
	@Override
	public int deleteByPrimaryKey(String matId) {
		// TODO Auto-generated method stub
		Long num=0L;
		int rows=0;
		Long ro=dmDao.findByMatId(matId);
		if(ro>0){
			rows=-1;
		}else{
			int count=repDao.deleteByPwId(matId,num);
			if(count>0){
				rows=materialsDao.deleteByPrimaryKey(matId);
			}
		}
		return rows;
	}

	/**
	 * 增加所有材料信息
	 */
	@Override
	public int insert(MaterialsModel record) {
		// TODO Auto-generated method stub
		return materialsDao.insert(record);
	}

	/**
	 * 增加部分材料信息
	 */
	@Override
	public int insertSelective(MaterialsModel record) {
		// TODO Auto-generated method stub
		//生成ID替换横线获取前十位
		String id=Tools.getRandomUUID();
		record.setMatId(id);
		record.setIsva(Tools.ISVA_YES);
		//获取当前日期时间
		Timestamp ss = new Timestamp(new Date().getTime());
		record.setCztime(ss);
		//增加材料信息
		int count=materialsDao.insertSelective(record);
		if(count>0){
			/*
			 * 增加仓库信息
			 */
			RepertoryModel rep=new RepertoryModel();
			String repId=Tools.getRandomUUID();
			rep.setRepId(repId);
			rep.setOptime(ss);
			rep.setRepNumber(0L);
			rep.setIsva(Tools.ISVA_YES);
			rep.setPwId(id);
			rep.setFilId("1");
			repDao.insertSelective(rep);
		}
		return count;
	}
	
	/**
	 * 根据材料ID查询所有
	 */
	@Override
	public MaterialsModel selectByPrimaryKey(String matId) {
		// TODO Auto-generated method stub
		MaterialsModel mat=materialsDao.selectByPrimaryKey(matId);
		System.out.println("修改材料价格+"+mat.getMatPrice());
		String mon=(String.valueOf(mat.getMatPrice()));
		mat.setMoney(mon);
		return mat;
	}
	
	/**
	 * 根据材料ID修改部分信息
	 */
	@Override
	public int updateByPrimaryKeySelective(MaterialsModel record) {
		//获取当前时间
		Timestamp ss = new Timestamp(new Date().getTime());
		record.setCztime(ss);
		// TODO Auto-generated method stub
		return materialsDao.updateByPrimaryKeySelective(record);
	}

	/**
	 * 根据材料ID修改所有信息
	 */
	@Override
	public int updateByPrimaryKey(MaterialsModel record) {
		// TODO Auto-generated method stub
		return materialsDao.updateByPrimaryKey(record);
	}

	/**
	 *查询所有
	 */
	@Override
	public  Map<String,Object> findAll(String limit,String page,String matName,String drugId,String typeIds) {
		//NEW一个材料的模型,加入页面信息和类型名称
		MaterialsModel materialsModel = new MaterialsModel();
		Page pa=new Page();
		pa.setPage(Integer.valueOf(page));
		pa.setRows(Integer.valueOf(limit));
		materialsModel.setPage(pa);
		materialsModel.setMatName(matName);
		materialsModel.setMatTypeId(typeIds);
		//查询所有信息或者按条件查询
		List<MaterialsModel> list=materialsDao.findAll(materialsModel);
		//List<DrugMaterialsModel> dmList=dmDao.findMatIdByDrugId(drugId);
		
		for (int j=0; j<list.size(); j++) {
			System.out.println("材料价格+"+list.get(j).getMatPrice());
			String mon=(String.valueOf(list.get(j).getMatPrice()));
			list.get(j).setMoney(mon);
	        /*
	         * 查询材料类型名称
	         */
	        String typeId=list.get(j).getMatTypeId();
	        if(!Tools.isEmpty(typeId)){
	        	MaterialTypeModel matType=materialTypeDao.selectByPrimaryKey(typeId);
	        	if(matType!=null){
	        		System.out.println("材料类型名称"+matType.getMatTypeName());
		  	        list.get(j).setTypeName(matType.getMatTypeName());
	        	}
	        }
	        /*for(int i=0; i<dmList.size(); i++){
	        	System.out.println("材料ID1 "+dmList.get(i).getMatId()+" 材料ID2 "+list.get(j).getMatId());
	        	if(dmList.get(i).getMatId().equals(list.get(j).getMatId())){
	        		list.get(j).setLAY_CHECKED("true");
	        	}
	        }*/
		}
		//查询出的信息的总行数
		Long count = materialsDao.findRows(materialsModel);
		System.out.println("总行数"+count);
		//加入MAP集合
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("code",0);
		map.put("msg","");
		map.put("count",count);
		map.put("data", list);
		System.out.println("==="+new Gson().toJson(list));
		return map;
		
	}
	
	/**
	 * 查询总行数
	 */
	@Override
	public long findRows(MaterialsModel record) {
		// TODO Auto-generated method stub
		return materialsDao.findRows(record);
	}

}
